
#include "IPC_Pipe.h"


// fd[0] write pipe fd[1] read pipe
// one-way
int IPC_Pipe(int fd[2])
{
   if (pipe(fd) < 0)
    {
        return -1;
    }
    return 0;
}


int IPC_PipeRead(int fd[2], char *data, int len)
{
    close(fd[1]);
    return read(fd[0], data, len);

}

int IPC_PipeWrite(int fd[2],  char *data, int len)
{
    close(fd[0]);
    return write(fd[1], data, len);

}

void IPC_PipeClose(int fd)
{
    close(fd);

}



int IPC_NamePipe(char *fifoname)
{
    int res;
    if(access (fifoname , F_OK )  ==  - 1 ) 
    {
        res  = mkfifo (fifoname ,  0777 );
        if(res != 0)
           return -1;
    }
    return 0;
}

int IPC_NamePipeDetele(char *fifoname)
{
    if(access (fifoname , F_OK )  ==  0 ) 
    {
        unlink(fifoname);
    }
    return 0;
}


int IPC_NamePipeOpenRead(char *fifoname)
{
    IPC_NamePipe(fifoname);
    return open (fifoname , O_RDONLY) ;
}

int IPC_NamePipeOpenWrite(char *fifoname)
{
    IPC_NamePipe(fifoname);
    return open (fifoname , O_WRONLY) ;
}

int IPC_NamePipeRead(int pipe_fd, char *data, int len)
{
    return read(pipe_fd , data , len );
}


int IPC_NamePipeWrite(int pipe_fd, char *data, int len)
{
    return write(pipe_fd, data, len);

}
